package com.lej.app_interact.person

import android.view.Gravity
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.alibaba.android.arouter.facade.annotation.Route
import com.lej.app_interact.*
import com.lej.app_interact.databinding.InteractActivityInteractPersonBinding
import com.lej.core.base.BaseActivity
import com.lej.core.base.statusHeight
import com.lej.core.dsl.margin_top_px
import com.lej.core.router.CRouter
import com.lej.core.utils.extention.*
import com.lej.core.utils.load
import com.lej.core.widget.drawable.CustomDrawable
import com.lej.dataservice.club.bean.interact.CreatorSearchInfo
import com.qmuiteam.qmui.kotlin.onClick
import com.qmuiteam.qmui.util.QMUIStatusBarHelper
import com.qmuiteam.qmui.widget.tab.QMUITabBuilder
import kotlinx.coroutines.launch

@Route(path = CRouter.interactPerson)
class InteractPersonActivity :
    BaseActivity<InteractActivityInteractPersonBinding, InteractPersonViewModel>() {
    private var index = 0
    private var id: String? = null
    private lateinit var tvDrawable: CustomDrawable

    override fun getLayout(): Int = R.layout.interact_activity_interact_person

    override fun initVariableId(): Int = BR.vm

    override fun initData() {
        super.initData()
        id = intent.getStringExtra(INTERACT_PERSON_ID)
        QMUIStatusBarHelper.translucent(this)
        QMUIStatusBarHelper.setStatusBarLightMode(this)
        val tabs = arrayOf(PersonTabs.KNOW_ME, PersonTabs.PRODUCT)
        binding.vp.adapter = MainAdapter(this, tabs)
        binding.vp.currentItem = index
        val tabBuilder: QMUITabBuilder = binding.tabs.tabBuilder().setGravity(Gravity.CENTER)
        tabs.map { binding.tabs.addTab(tabBuilder.setText(it.title).build(this)) }
        binding.tabs.setupWithViewPager(binding.vp)
        binding.rl.margin_top_px = statusHeight
        binding.ivBack.onClick {
            onBackPressed()
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        id?.let {
            lifecycleScope.launch {
                getPersonData(it)
            }
        }
        binding.tvFollow.onClick { followClick() }

        viewModel.isFollow.observe(this) {
            binding.tvFollow.background = if (it == HAS_FALLOW) null else tvDrawable
            binding.tvFollow.text =
                if (it == HAS_FALLOW) R.string.interact_person_follow.resString else R.string.interact_person_un_follow.resString
        }
    }

    private fun followClick() {
        if (viewModel.isFollow.value == HAS_FALLOW) {
            "暂不支持取消关注".toast()
        } else {
            lifecycleScope.launch {
                showLoading()
                id?.toInt()?.let { it1 ->
                    val success = viewModel.followCreate(it1)
                    if (success) viewModel.isFollow.value = HAS_FALLOW
                }
                disDialog()
            }
        }
    }

    private suspend fun getPersonData(id: String) {
        binding.flContainer.showPageStateLoading()
        val info: CreatorSearchInfo? = viewModel.getCreatorInfo(id)
        info?.let {
            binding.flContainer.restorePageState()
            load(
                info.avatar,
                R.drawable.ic_default_icon,
                binding.ivIcon
            )
            if (info.cover?.isNotEmpty() == true) load(
                info.cover,
                R.drawable.ic_default_icon,
                binding.ivBg
            )
            if (info.follow == NOT_FOLLOW) {
                tvDrawable =
                    CustomDrawable(
                        strokeWidth = 2,
                        strokeColor = R.color.themeWhite,
                        cornerRadius = 10
                    )
            }
            binding.tvName.text = info.nickname
            binding.tvTitle.text = info.nickname
            viewModel.isFollow.value = info.follow
            return
        }
        binding.scrollView.visibility = View.GONE
        binding.flContainer.showPageState(PageState.Error("啊哦 加载出现了点小问题～") {
            it.clickLaunch {
                getPersonData(id)
            }
        })
    }

    class MainAdapter(fm: FragmentActivity, private val tabs: Array<PersonTabs>) :
        FragmentStateAdapter(fm) {
        override fun getItemCount(): Int {
            return tabs.size
        }

        override fun createFragment(position: Int): Fragment {
            return tabs[position].fragment()
        }
    }

}